package de.karlNet.sshTray.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.karlNet.sshTray.controller.TypeController;
import de.karlNet.sshTray.datasources.IChangeListener;
import de.karlNet.sshTray.gui.tableModels.TypeTableModel;
import de.karlNet.sshTray.model.Type;

@Component
public class TypeForm extends JFrame implements IChangeListener {
	@Autowired
	private TypeController typeController;

	private JTextField txtTypename;
	private TypeTableModel tableModel;
	private JTextField txtScripttoexecute;
	private JTextField txtId;

	@javax.annotation.PostConstruct
	public void init() {
		this.dataHasBeenUpdated();
	}
	
	
	public TypeForm() {
		

		setTitle("Manage Types");
		setSize(600, 200);

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		JPanel center = new JPanel();
		getContentPane().add(center, BorderLayout.CENTER);
		final JTable table = new JTable(tableModel = new TypeTableModel());
		this.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);

		final JPanel north = new JPanel();
		getContentPane().add(north, BorderLayout.NORTH);
		GridBagLayout gbl_north = new GridBagLayout();
		gbl_north.columnWidths = new int[] { 0, 0, 0, 0, 0 };
		gbl_north.rowHeights = new int[] { 0, 0, 0, 0, 0 };
		gbl_north.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_north.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		north.setLayout(gbl_north);

		JLabel lblTypeName = new JLabel("Type name");
		GridBagConstraints gbc_lblTypeName = new GridBagConstraints();
		gbc_lblTypeName.anchor = GridBagConstraints.WEST;
		gbc_lblTypeName.insets = new Insets(0, 0, 5, 5);
		gbc_lblTypeName.gridx = 0;
		gbc_lblTypeName.gridy = 0;
		north.add(lblTypeName, gbc_lblTypeName);

		txtTypename = new JTextField();
		txtTypename.setText("typeName");
		GridBagConstraints gbc_txtTypename = new GridBagConstraints();
		gbc_txtTypename.gridwidth = 3;
		gbc_txtTypename.insets = new Insets(0, 0, 5, 0);
		gbc_txtTypename.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTypename.gridx = 1;
		gbc_txtTypename.gridy = 0;
		north.add(txtTypename, gbc_txtTypename);
		txtTypename.setColumns(10);

		JLabel lblScriptToExecute = new JLabel("Script to execute");
		GridBagConstraints gbc_lblScriptToExecute = new GridBagConstraints();
		gbc_lblScriptToExecute.anchor = GridBagConstraints.WEST;
		gbc_lblScriptToExecute.insets = new Insets(0, 0, 5, 5);
		gbc_lblScriptToExecute.gridx = 0;
		gbc_lblScriptToExecute.gridy = 1;
		north.add(lblScriptToExecute, gbc_lblScriptToExecute);

		txtScripttoexecute = new JTextField();
		txtScripttoexecute.setText("scriptToExecute");
		GridBagConstraints gbc_txtScripttoexecute = new GridBagConstraints();
		gbc_txtScripttoexecute.insets = new Insets(0, 0, 5, 0);
		gbc_txtScripttoexecute.gridwidth = 3;
		gbc_txtScripttoexecute.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtScripttoexecute.gridx = 1;
		gbc_txtScripttoexecute.gridy = 1;
		north.add(txtScripttoexecute, gbc_txtScripttoexecute);
		txtScripttoexecute.setColumns(10);

		JLabel lblId = new JLabel("ID");
		GridBagConstraints gbc_lblId = new GridBagConstraints();
		gbc_lblId.anchor = GridBagConstraints.WEST;
		gbc_lblId.insets = new Insets(0, 0, 5, 5);
		gbc_lblId.gridx = 0;
		gbc_lblId.gridy = 2;
		north.add(lblId, gbc_lblId);

		txtId = new JTextField();
		txtId.setEnabled(false);
		txtId.setEditable(false);
		txtId.setText("ID");
		GridBagConstraints gbc_txtId = new GridBagConstraints();
		gbc_txtId.gridwidth = 3;
		gbc_txtId.insets = new Insets(0, 0, 5, 5);
		gbc_txtId.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtId.gridx = 1;
		gbc_txtId.gridy = 2;
		north.add(txtId, gbc_txtId);
		txtId.setColumns(10);

		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.anchor = GridBagConstraints.EAST;
		gbc_panel_1.fill = GridBagConstraints.VERTICAL;
		gbc_panel_1.gridx = 3;
		gbc_panel_1.gridy = 3;
		north.add(panel_1, gbc_panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton btnOk = new JButton("OK");
		panel_1.add(btnOk);

		JButton btnCancel = new JButton("Cancel");
		panel_1.add(btnCancel);

		final JPopupMenu popupMenu = new JPopupMenu();
		JMenuItem create = new JMenuItem("Create");
		create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				north.setVisible(true);
			}
		});
		JMenuItem delete = new JMenuItem("Delete");
		delete.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				int selRow = table.getSelectedRow();
				if (selRow < 0) {
					return;
				}
				typeController.deleteType(tableModel.getTypeAt(selRow));
			}
		});
		JMenuItem edit = new JMenuItem("edit");

		JMenuItem connect = new JMenuItem("Connect...");
		connect.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				int selRow = table.getSelectedRow();
				if (selRow < 0) {
					return;
				}
			}
		});
		JMenuItem moveUp = new JMenuItem("Move Up");
		moveUp.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				int selRow = table.getSelectedRow();
				if (selRow < 0) {
					return;
				}
			}
		});
		JMenuItem moveDown = new JMenuItem("Move down");
		moveDown.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				int selRow = table.getSelectedRow();
				if (selRow < 0) {
					return;
				}
			}
		});

		popupMenu.add(create);
		popupMenu.add(delete);
		popupMenu.add(edit);
		popupMenu.add(connect);
		popupMenu.add(moveUp);
		popupMenu.add(moveDown);
		table.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				showPopup(e);
			}

			public void mouseReleased(MouseEvent e) {
				showPopup(e);
			}

			private void showPopup(MouseEvent e) {
				if (e.isPopupTrigger()) {
					popupMenu.show(e.getComponent(), e.getX(), e.getY());
				}
			}

		});
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				typeController.createOrUpdateType(new Type()
						.setCmdToExecute(txtScripttoexecute.getText())
						.setTitle(txtTypename.getText()).setId(txtId.getText()));
				clearInput();
				north.setVisible(false);
			}
		});

		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				clearInput();
				north.setVisible(false);
			}
		});
		edit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				north.setVisible(true);
				int selRow = table.getSelectedRow();
				if (selRow < 0) {
					return;
				}
				Type con = tableModel.getTypeAt(selRow);
				// load data
				txtId.setText(con.getId() + "");
				txtScripttoexecute.setText(con.getCmdToExecute());
				txtTypename.setText(con.getTitle());
			}
		});
		clearInput();
		north.setVisible(false);
	}

	private void clearInput() {
		this.txtScripttoexecute.setText("");
		this.txtTypename.setText("");
		this.txtId.setText("");
	}

	public void dataHasBeenUpdated() {
		this.tableModel.refresh(this.typeController.readTypes());
	}
}
